package com.itboye.ihomebank.adapter;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.QianYueManageBean;
import com.itboye.ihomebank.bean.RedDotBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.hetong.ActivityEditHeTongFangdong;
import com.itboye.ihomebank.activity.hetong.ActivityHuaBan;
import com.itboye.ihomebank.activity.hetong.TianXieHeTongActivityFangDong;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;
import com.itboye.ihomebank.web.WebActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/***
 * 签约管理
 * @author dsawe
 */
public class QianYueAdapter extends MutiplyCommonAdapter<QianYueManageBean>
        implements Observer {
    private List<QianYueManageBean> orderQueryModel;
    Activity context;
    private int type;
    protected String uid;
    protected String password;
    UserPresenter userPresenter;
    HousePresenter mHousePresenter;
    protected String tempContractNo;

    public QianYueAdapter(Activity context, List<QianYueManageBean> datas,
                          int... layoutId) {
        super(context, datas, layoutId);
        this.orderQueryModel = datas;
        this.context = context;
        uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
        password = SPUtils.get(context, null, SPContants.PASSWORD, "") + "";
        userPresenter = new UserPresenter(this);
        mHousePresenter = new HousePresenter(this);
        orderQueryModel = datas;
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.queRenHeTong_success) {
                ByAlert.alert(entity.getData());
                Iterator<QianYueManageBean> iterator = orderQueryModel
                        .iterator();
                while (iterator.hasNext()) {
                    QianYueManageBean bean = iterator.next();
                    if (bean.getContractNo().equals(tempContractNo)) {
                        //判断红点
                        mHousePresenter.queryRedDot(uid, MyApplcation.device_token);
                        iterator.remove();
                        notifyDataSetChanged();
                        break;
                    }
                }
            } else if (entity.getEventType() == UserPresenter.queRenHeTong_fail){
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == HousePresenter.cancelContract_success){
                ByAlert.alert(entity.getData());
                mHousePresenter.qianYueGuanLi(uid, 0 + "");
            } else if (entity.getEventType() == HousePresenter.cancelContract_fail){
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == HousePresenter.qianYueGuanLi_success) {
                orderQueryModel.clear();
                orderQueryModel.addAll((List<QianYueManageBean>) entity.getData()) ;
                notifyDataSetChanged();
            }else if (entity.getEventType() == HousePresenter.qianYueGuanLi_fail){
                ByAlert.alert(entity.getMsg());
            }else  if (entity.getEventType() == HousePresenter.queryRedDot_success){
                Log.d("hongdian=","true");
                RedDotBean mRedDotBean= (RedDotBean) entity.getData();
                if (mRedDotBean.getNeed_write_facility_count()>0){
                    SPUtils.put(context,null,SPContants.FUJIAN_ORDER,true);
                    context.sendBroadcast(new Intent("showRedDot"));
                }

            }else if (entity.getEventType() == HousePresenter.queryRedDot_fail){
//                Log.d("hongdian=","false");
//                ByAlert.alert("红点数据请求失败");
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void getItemPosition(int position) {
        // TODO Auto-generated method stub

    }

    @Override
    public void convert(ViewHolder holder, final QianYueManageBean t, int type,
                        int position) {
        int fangDongStatus = Integer.parseInt(t.getLesserWritten());//房东有没有填写(1为签)
        int fakeStatus = Integer.parseInt(t.getRenterWritten());//房客有没有填写(1为签)
        String shenfen = t.getUserType();
        if (shenfen.equals("lesser")) {// 房东
            if (fakeStatus == 0 && fangDongStatus == 0) {
                    holder.setText(R.id.all_left, "填写合同");
                    holder.setText(R.id.all_right, "查看合同");
                    holder.setVisible(R.id.all_center, false);
                    holder.setVisible(R.id.all_left, true);
                } else if (fakeStatus == 0 && fangDongStatus == 1) {
                    holder.setText(R.id.all_left, "修改合同");
                    holder.setText(R.id.all_right, "查看合同");
                    holder.setVisible(R.id.all_center, false);
                    holder.setVisible(R.id.all_left, true);
                } else if (fakeStatus == 1 && fangDongStatus == 1) {
                    holder.setText(R.id.all_center, "确认完成");
                    holder.setText(R.id.all_left, "修改合同");
                    holder.setText(R.id.all_right, "查看合同");
                    holder.setVisible(R.id.all_center, true);
                    holder.setVisible(R.id.all_left, true);
            }
            if (t.getRenter_confirm().equals("0")){
                holder.setVisible(R.id.cancelContractTv,true);
                holder.setText(R.id.cancelContractTv,"取消合同");
                ((TextView)holder.getView(R.id.cancelContractTv))
                        .setBackground(context.getResources().getDrawable(R.drawable.shape_overdue_red));
            }else {
                holder.setVisible(R.id.cancelContractTv,false);

            }
        } else {// 租客
            holder.setVisible(R.id.cancelContractTv,false);
            if (fakeStatus == 0 && fangDongStatus == 0) {
                holder.setVisible(R.id.all_left, false);
                holder.setVisible(R.id.all_center, false);
                holder.setText(R.id.all_right, "查看合同");
            } else if (fakeStatus == 0 && fangDongStatus == 1) {
                holder.setVisible(R.id.all_left, true);
                holder.setText(R.id.all_left, "签订合同");
                holder.setText(R.id.all_right, "查看合同");
                holder.setVisible(R.id.all_center, false);
            } else if (fakeStatus == 1 && fangDongStatus == 1) {
                holder.setVisible(R.id.all_left, true);
                holder.setText(R.id.all_center, "确认完成");
                holder.setText(R.id.all_left, "重新签名");
                holder.setText(R.id.all_right, "查看合同");
                holder.setVisible(R.id.all_center, true);
            }
        }
        holder.setText(R.id.txt_name, t.getTitle());
        holder.setText(R.id.tv_shuxing, t.getHouseRoom() + "室/" + t.getHouseHall() + "厅/"
                + t.getHouseArea() + "㎡");
        holder.setText(R.id.hetong, "合同号:" + t.getContractNo());

        XImageLoader.load(NetPublicConstant.IMAGE_URL + t.getImgId(),
                (ImageView) holder.getView(R.id.img_house));
        holder.setText(R.id.txt_zujin, Double.parseDouble(t.getRent()) / 100
                + "元/月");
        holder.setOnClickListener(R.id.all_left, position,
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String shenfen = t.getUserType();
                        TextView hetong = (TextView) v;
                        String text = hetong.getText().toString();
                        if (text.equals("填写合同")) {
                            if (!shenfen.equals("renter")) {// 房东
                                Intent intent = new Intent(context,
                                        TianXieHeTongActivityFangDong.class);
                                intent.putExtra("contractNo", t.getContractNo());

                                context.startActivity(intent);
                            } else {
                                Intent intent = new Intent(//直接跳转至画板
                                        context,
                                        ActivityHuaBan.class);
                                context.startActivity(intent);
                            }
                        } else if (text.equals("修改合同")) {
                            if (shenfen.equals("renter")) {// 租客
                                if(t.getLesser_confirm().equals("1")||t.getRenter_confirm().equals("1")){
                                    ByAlert.alert("已经确认");
                                }else{
                                    Intent intent = new Intent(//直接跳转至画板
                                            context,
                                            ActivityHuaBan.class);
                                    intent.putExtra("contractNo", t.getContractNo());
                                    context.startActivity(intent);
                                }
                            } else {
                                if(t.getLesser_confirm().equals("1")||t.getRenter_confirm().equals("1")){
                                    ByAlert.alert("已经确认");
                                }else{
                                    Intent intent = new Intent(context,
                                            ActivityEditHeTongFangdong.class);
                                    intent.putExtra("contractNo", t.getContractNo());
                                    intent.putExtra("shenfen", "lesser");
                                    context.startActivity(intent);
                                }
                            }
                        } else if (text.equals("签订合同")) {
                            Intent intent = new Intent(//直接跳转至画板
                                    context,
                                    ActivityHuaBan.class);
                            intent.putExtra("contractNo", t.getContractNo());
                            context.startActivity(intent);
                        }else if (text.equals("重新签名")) {
                            Intent intent = new Intent(//直接跳转至画板
                                    context,
                                    ActivityHuaBan.class);
                            intent.putExtra("contractNo", t.getContractNo());
                            context.startActivity(intent);
                        }
                    }
                });
        holder.setOnClickListener(R.id.all_right, position,
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(context, WebActivity.class);
                        intent.putExtra("url", NetPublicConstant.HETONG_URL
                                + "?uid=" + uid + "&psw=" + password
                                + "&contract_no=" + t.getContractNo() + "&pc=1");
                        intent.putExtra("title", "查看合同");
                        context.startActivity(intent);
                    }
                });
        holder.setOnClickListener(R.id.all_center, position,
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        tempContractNo = t.getContractNo();
                        userPresenter.wanChengHeTong(uid, t.getContractNo());


                        // Intent intent = new Intent(context,
                        // WebActivity.class);
                        // intent.putExtra("url", NetPublicConstant.HETONG_URL
                        // + "?uid=" + uid + "&psw=" + password
                        // + "&contract_no=" + t.getContractNo());
                        // context.startActivity(intent);
                    }
                });

        holder.setOnClickListener(R.id.cancelContractTv, position,
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        tempContractNo = t.getContractNo();
                        mHousePresenter.cancleContract(uid,tempContractNo);
                    }
                });
    }


    public void setStatus(int type) {
        this.type = type;
    }
}
